package classconverstation.cscl.ist.psu.edu.client.classdisplay;


import java.util.ArrayList;

import classconverstation.cscl.ist.psu.edu.client.messengers.ClassCommonsMessage;
import classconverstation.cscl.ist.psu.edu.client.services.ClassCommonsService;
import classconverstation.cscl.ist.psu.edu.client.services.ClassCommonsServiceAsync;
import classconverstation.cscl.ist.psu.edu.client.util.ClassConversationUtil;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasAllMouseHandlers;
import com.google.gwt.event.dom.client.MouseDownEvent;
import com.google.gwt.event.dom.client.MouseDownHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Pin extends Composite implements HasAllMouseHandlers {

	private ClassCommonsMessage msg;
	private VerticalPanel mainPanel;
	private Pin thisPin = this;

	Image check = new Image("../images/check.png");
	Image close = new Image("../images/close.png");
	Image video = new Image("../images/youtube.png");
	Image image = new Image("../images/image.png");
	public Pin(ClassCommonsMessage msg){
		this.msg = msg;

		this.mainPanel = new VerticalPanel();
		initWidget(mainPanel);
		//mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		Label qLabel = new Label(msg.getContent());
		qLabel.addStyleDependentName("qLabel");
		mainPanel.add(qLabel);

		addMultiMedia();		
		addOptions();

		mainPanel.addStyleName("pin");
	}

	public void addMultiMedia(){
		FlexTable multimediaTable = new FlexTable();
		int column = 0;

		if (!ClassConversationUtil.IsNullOrEmpty(msg.getImageURL())){
			multimediaTable.setWidget(0, column, image);
			column++;
			image.addStyleName("pointer");
			image.addClickHandler(new ClickHandler(){
				public void onClick(ClickEvent event) {
					PopupPanel imagePop = new PopupPanel();
					Image fullImage = new Image(msg.getImageURL());
					imagePop.setWidget(fullImage);
					imagePop.setAutoHideEnabled(true);
					imagePop.center();
					
				}
				
			});
			
		}
		if (!ClassConversationUtil.IsNullOrEmpty(msg.getVideoURL())){
			multimediaTable.setWidget(0, column, video);
			video.addStyleName("pointer");
			
			video.addClickHandler(new ClickHandler(){

				public void onClick(ClickEvent event) {
					String vid = msg.getVideoID();
					System.out.println("Video ID "+vid);
					Frame frame = new Frame("http://www.youtube.com/embed/"+vid);
					frame.setPixelSize(420, 315);
					
					PopupPanel videoPop = new PopupPanel();
					videoPop.setWidget(frame);
					videoPop.setAutoHideEnabled(true);
					videoPop.center();					
				}
				
			});
		}

		mainPanel.add(multimediaTable);
	}


	public void addOptions(){
		FlowPanel  fPanel =  new FlowPanel();

		FlowPanel  metaPanel = new FlowPanel();
		FlowPanel  optionsPanel = new FlowPanel();

		metaPanel.addStyleName("metaPanel");
		FlexTable metaTable = new FlexTable();
		Label nameLabel = new Label(msg.getPoster().getFName());
		DateTimeFormat fmt = DateTimeFormat.getFormat("hh:mm aaa, MM/dd, yyyy");
		Label timeLabel = new Label(fmt.format(msg.getPostDate()));
		metaTable.setWidget(0, 0, nameLabel);
		metaTable.setWidget(1, 0, timeLabel);
		metaPanel.add(metaTable);

		FlexTable options = new FlexTable();
		options.setWidget(0, 0, check);
		options.setWidget(0, 1, close);
		check.addStyleName("pointer");
		close.addStyleName("pointer");

		close.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				thisPin.setVisible(false);				
			}

		});

		check.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				updatePinState(4);
			}

			private void updatePinState(int finalState) {
				ClassCommonsServiceAsync classCommonsServiceSvc = GWT.create(ClassCommonsService.class);

				AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
					public void onFailure(Throwable caught) {
						caught.printStackTrace();
					}
					public void onSuccess(Boolean updated) {
						if (updated){
							mainPanel.addStyleName("solved");
						}
					}
				};
				classCommonsServiceSvc.updatePinState(msg, finalState, callback);   
				
				
			}
		});
		optionsPanel.addStyleName("optionsPanel");
		optionsPanel.add(options);

		fPanel.add(metaPanel);
		fPanel.add(optionsPanel);
		mainPanel.add(fPanel);
	}

	public Image getCloseButton(){
		return this.close;
	}
	
	public void setAsSolved(){
		mainPanel.addStyleName("solved");
	}



	public HandlerRegistration addMouseDownHandler(MouseDownHandler handler) {
		return addDomHandler(handler, MouseDownEvent.getType());
	}


	public HandlerRegistration addMouseUpHandler(MouseUpHandler handler) {
		return addDomHandler(handler, MouseUpEvent.getType());
	}


	public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) {
		return addDomHandler(handler, MouseOutEvent.getType());
	}

	public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) {
		return addDomHandler(handler, MouseOverEvent.getType());
	}


	public HandlerRegistration addMouseMoveHandler(MouseMoveHandler handler) {
		return addDomHandler(handler, MouseMoveEvent.getType());
	}


	public HandlerRegistration addMouseWheelHandler(MouseWheelHandler handler) {
		return addDomHandler(handler, MouseWheelEvent.getType());
	}

	/*
	@Override
	public HandlerRegistration addClickHandler(ClickHandler handler) {
		return addDomHandler(handler, ClickEvent.getType());
	}
	 */

}
